package com.idealighter.game.games.orderma.util;

import com.idealighter.game.core.tuple.Pair;
import com.idealighter.game.dictionary.dic.OrdermaExtraDic;
import com.idealighter.game.dictionary.dic.OrdermaMultipleDic;
import com.idealighter.game.dictionary.dic.OrdermaRoomDic;
import com.idealighter.game.dictionary.domain.OrdermaExtraDomain;
import com.idealighter.game.games.orderma.dto.HuPaiItemInfo;
import com.idealighter.game.games.orderma.struct.OderMaHuPaiType;
import com.idealighter.game.games.orderma.struct.OderMaHuType;
import com.idealighter.game.games.orderma.struct.OderMaSeatCards;
import com.idealighter.game.server.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;

/**
 * 血战到底计算器，计算翻数 .
 * 
 * @date 2016年9月21日 下午4:18:04
 *
 */
public class OderMaCalculator {

  private OderMaCalculator() {}

  /**
   * 计算胡牌加翻明细数据(胡的类型、胡牌的牌型、清一色、四合) .
   * 
   * @param cards .
   * @param huType . 胡的类型
   * @param huPaiType . 胡的牌的类型
   * @return totalMultiple,items
   */
  public static Pair<Integer, List<HuPaiItemInfo>> calcHuData(OderMaSeatCards cards,
      OderMaHuType huType, OderMaHuPaiType huPaiType, boolean isZm, int mjId) {
    OrdermaMultipleDic multipleDic = ApplicationContext.getBean(OrdermaMultipleDic.class);
    // 基础金币
    int baseGold = ApplicationContext.getBean(OrdermaRoomDic.class)
        .get(cards.seat.table.room.getId().intValue()).getMaxOne();
    List<HuPaiItemInfo> items = new ArrayList<>();
    // 合计胡的翻数
    int totalMultiple = 0;

    /*
     * 胡的类型计算(1:平胡,2:天胡,3:地胡,4:抢杠胡,5:杠上花,6:杠上炮) .
     */

    /*
     * 胡牌的牌型计算(7:小七对,8:大对,9:龙对) .
     */
    if (huPaiType != null && huPaiType != OderMaHuPaiType.GENERAL) {
      HuPaiItemInfo huPaiTypeItem = new HuPaiItemInfo();
      int huPaiMultiple = huPaiType.multiple();
      int huPaiGold = huPaiMultiple * baseGold;
      huPaiTypeItem.setType(huPaiType.multipleId());
      huPaiTypeItem.setMultiple(huPaiMultiple);
      huPaiTypeItem.setGolds(huPaiGold);
      items.add(huPaiTypeItem);

      totalMultiple += huPaiMultiple;
    }

    /*
     * 清一色计算(10:清一色) .
     */
    if (cards.isQingYiSe()) {
      int qingYiSeMultiple = multipleDic.get(OderMaHuPaiType.QING_YI_SE.multipleId()).getMultiple();
      int qingYiSeGold = baseGold * qingYiSeMultiple;
      HuPaiItemInfo qingYiSeItem = new HuPaiItemInfo();
      qingYiSeItem.setType(OderMaHuPaiType.QING_YI_SE.multipleId());
      qingYiSeItem.setMultiple(qingYiSeMultiple);
      qingYiSeItem.setGolds(qingYiSeGold);
      items.add(qingYiSeItem);

      totalMultiple += qingYiSeMultiple;
    }

    /*
     * 四合计算(11:四合) .
     */
    int siHeMultiple = multipleDic.get(OderMaHuPaiType.SI_HE.multipleId()).getMultiple();
    int siHeGold = baseGold * siHeMultiple;
    int siHeCount = cards.siHeCount(huType, huPaiType);

    for (int i = 0; i < siHeCount; i++) {
      HuPaiItemInfo siHeItem = new HuPaiItemInfo();
      siHeItem.setType(OderMaHuPaiType.SI_HE.multipleId());

      siHeItem.setMultiple(siHeMultiple);
      siHeItem.setGolds(siHeGold);
      items.add(siHeItem);

      totalMultiple += siHeMultiple;
    }

    // TODO 添加其他胡牌脾形式 ->此处只是计算每项的金额，并未做总体计算
    // 断九幺
    if (cards.isDuanjiuYao()) {
      int multiple = multipleDic.get(OderMaHuPaiType.DUAN_JIU_YAO.multipleId()).getMultiple();
      int gold = baseGold * multiple;
      HuPaiItemInfo menqingItem = new HuPaiItemInfo();
      menqingItem.setType(OderMaHuPaiType.DUAN_JIU_YAO.multipleId());
      menqingItem.setMultiple(multiple);
      menqingItem.setGolds(gold);
      items.add(menqingItem);
      totalMultiple += multiple;
    }

    // 一色二顺 双铺子
    if (cards.isYiSeErShun()) {
      int multiple = multipleDic.get(OderMaHuPaiType.SHUANG_PU_ZI.multipleId()).getMultiple();
      int gold = baseGold * multiple;
      HuPaiItemInfo item = new HuPaiItemInfo();
      item.setType(OderMaHuPaiType.SHUANG_PU_ZI.multipleId());
      item.setMultiple(multiple);
      item.setGolds(gold);
      items.add(item);
      totalMultiple += multiple;
    }
    // 胡砍张
    if (cards.isHuKan(mjId)) {
      int multiple = multipleDic.get(OderMaHuPaiType.HU_KAN_ZHANG.multipleId()).getMultiple();
      int gold = baseGold * multiple;
      HuPaiItemInfo item = new HuPaiItemInfo();
      item.setType(OderMaHuPaiType.HU_KAN_ZHANG.multipleId());
      item.setMultiple(multiple);
      item.setGolds(gold);
      items.add(item);
      totalMultiple += multiple;
    }
    // 门清
    if (cards.isMenQing(isZm)) {
      int multiple = multipleDic.get(OderMaHuPaiType.MEN_QING.multipleId()).getMultiple();
      int gold = baseGold * multiple;
      HuPaiItemInfo item = new HuPaiItemInfo();
      item.setType(OderMaHuPaiType.MEN_QING.multipleId());
      item.setMultiple(multiple);
      item.setGolds(gold);
      items.add(item);
      totalMultiple += multiple;
    }
    // 胡单吊
    if (cards.isHuDanDiao(mjId)) {
      int multiple = multipleDic.get(OderMaHuPaiType.HU_DAN_DIAO.multipleId()).getMultiple();
      int gold = baseGold * multiple;
      HuPaiItemInfo item = new HuPaiItemInfo();
      item.setType(OderMaHuPaiType.HU_DAN_DIAO.multipleId());
      item.setMultiple(multiple);
      item.setGolds(gold);
      items.add(item);
      totalMultiple += multiple;
    }
    // 一杠
    if (cards.isYiGang() > 0) {
      int multiple =
          multipleDic.get(OderMaHuPaiType.YI_GANG.multipleId()).getMultiple() * cards.isYiGang();
      int gold = baseGold * multiple;
      HuPaiItemInfo item = new HuPaiItemInfo();
      item.setType(OderMaHuPaiType.YI_GANG.multipleId());
      item.setMultiple(multiple);
      item.setGolds(gold);
      items.add(item);
      totalMultiple += multiple;
    }
    // 一起通贯
    if (cards.isYiGuanTong()) {
      int multiple = multipleDic.get(OderMaHuPaiType.YI_QI_TONG_GUAN.multipleId()).getMultiple();
      int gold = baseGold * multiple;
      HuPaiItemInfo item = new HuPaiItemInfo();
      item.setType(OderMaHuPaiType.YI_QI_TONG_GUAN.multipleId());
      item.setMultiple(multiple);
      item.setGolds(gold);
      items.add(item);
      totalMultiple += multiple;
    }
    // 绝支压裆
    if (cards.isJueZhiYaDang(mjId)) {
      int multiple = multipleDic.get(OderMaHuPaiType.JUE_ZHI_YA_DANG.multipleId()).getMultiple();
      int gold = baseGold * multiple;
      HuPaiItemInfo item = new HuPaiItemInfo();
      item.setType(OderMaHuPaiType.JUE_ZHI_YA_DANG.multipleId());
      item.setMultiple(multiple);
      item.setGolds(gold);
      items.add(item);
      totalMultiple += multiple;
    }
    // 平胡有翻的情况下显示为非平胡
    if (totalMultiple > 0 && huType == OderMaHuType.PING_HU) {
      //
    } else {
      HuPaiItemInfo huItem = new HuPaiItemInfo();
      // 胡的倍数
      int huMultiple = huType.multiple();
      int huGold = baseGold * huMultiple;
      huItem.setType(huType.multipleId());
      huItem.setMultiple(huMultiple);
      huItem.setGolds(huGold);
      items.add(huItem);
      totalMultiple += huMultiple;
    }

    return new Pair<>(totalMultiple, items);
  }

  /**
   * 计算玩家总收益 .
   * 
   * 
   * @param itemInfos .
   * @param roomId .
   * @return
   */
  public static long calcBillGold(Pair<Integer, List<HuPaiItemInfo>> itemInfos, int roomId) {

    OrdermaExtraDic multipleExtraDic = ApplicationContext.getBean(OrdermaExtraDic.class);

    int baseGold = ApplicationContext.getBean(OrdermaRoomDic.class).get(roomId).getMaxOne();

    int minGold = ApplicationContext.getBean(OrdermaRoomDic.class).get(roomId).getMinOne();
    // 额翻倍
    int extraMulti = 0;
    for (OrdermaExtraDomain exta : multipleExtraDic.list()) {
      if (itemInfos.v1 >= exta.getMin() && itemInfos.v1 <= exta.getMax()) {
        extraMulti = exta.getMultiple();
      }
    }
    // 有翻情况
    if (itemInfos.v1 > 1) {
      return (baseGold * itemInfos.v1 + minGold) * extraMulti;
    }

    return minGold;
  }

  /**
   * 计算胡的倍数,牌满足3n+2 .
   * 
   * @param cards .
   * @param huType .
   * @param huPaiType . 胡牌类型
   * @param mjId .
   * @return
   */
  public static int calcHuMultiple(OderMaSeatCards cards, OderMaHuType huType,
      OderMaHuPaiType huPaiType, int mjId) {
    OrdermaMultipleDic multipleDic = ApplicationContext.getBean(OrdermaMultipleDic.class);
    // 合计胡的翻数
    int totalMultiple = 0;

    /*
     * 胡的类型计算(1:平胡,2:天胡,3:地胡,4:抢杠胡,5:杠上花,6:杠上炮) .
     */
    totalMultiple += huType.multiple();

    /*
     * 胡牌的牌型计算(7:小七对,8:大对,9:龙对) .
     */
    if (huPaiType != null && huPaiType != OderMaHuPaiType.GENERAL) {
      totalMultiple += huPaiType.multiple();
    }

    /*
     * 清一色计算(10:清一色) .
     */
    int qingYiSeMultiple = multipleDic.get(OderMaHuPaiType.QING_YI_SE.multipleId()).getMultiple();
    if (cards.isQingYiSe()) {
      totalMultiple += qingYiSeMultiple;
    }

    /*
     * 四合计算(11:四合) .
     */
    int siHeMultiple = multipleDic.get(OderMaHuPaiType.SI_HE.multipleId()).getMultiple();
    int siHeCount = cards.siHeCount(huType, huPaiType);

    for (int i = 0; i < siHeCount; i++) {
      totalMultiple += siHeMultiple;
    }

    if (cards.isDuanjiuYao()) {
      int multiple = multipleDic.get(OderMaHuPaiType.DUAN_JIU_YAO.multipleId()).getMultiple();
      totalMultiple += multiple;
    }

    // 一色二顺 双铺子
    if (cards.isYiSeErShun()) {
      int multiple = multipleDic.get(OderMaHuPaiType.SHUANG_PU_ZI.multipleId()).getMultiple();
      totalMultiple += multiple;
    }
    // 胡砍张
    if (cards.isHuKan(mjId)) {
      int multiple = multipleDic.get(OderMaHuPaiType.HU_KAN_ZHANG.multipleId()).getMultiple();
      totalMultiple += multiple;
    }
    // 门清
    if (cards.isMenQing(false)) {
      int multiple = multipleDic.get(OderMaHuPaiType.MEN_QING.multipleId()).getMultiple();
      totalMultiple += multiple;
    }
    // 胡单吊
    if (cards.isHuDanDiao(mjId)) {
      int multiple = multipleDic.get(OderMaHuPaiType.HU_DAN_DIAO.multipleId()).getMultiple();
      totalMultiple += multiple;
    }
    // 一杠
    if (cards.isYiGang() > 0) {
      int multiple =
          multipleDic.get(OderMaHuPaiType.YI_GANG.multipleId()).getMultiple() * cards.isYiGang();
      totalMultiple += multiple;
    }
    // 一起通贯
    if (cards.isYiGuanTong()) {
      int multiple = multipleDic.get(OderMaHuPaiType.YI_QI_TONG_GUAN.multipleId()).getMultiple();
      totalMultiple += multiple;
    }
    // 绝支压裆
    if (cards.isJueZhiYaDang(mjId)) {
      int multiple = multipleDic.get(OderMaHuPaiType.JUE_ZHI_YA_DANG.multipleId()).getMultiple();
      totalMultiple += multiple;
    }

    return totalMultiple;
  }

}
